﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
using System.Data.Entity;

namespace Server.DB
{
    public class Repository<T> : IRepository<T> where T : class
    {
        
        protected Context context;
        
        public Repository(Context dataContext)
        {
            context = dataContext;
        }
 
        public void Insert(T entity)
        {
            context.Set<T>().Add(entity);
            
        }
 
        public IQueryable<T> SearchFor(Expression<Func<T, bool>> predicate)
        {
            return context.Set<T>().Where(predicate);
        }
 
        public IQueryable<T> All
        {
            get { return context.Set<T>(); }
        }

        public T Find(int id)
        {
            return context.Set<T>().Find(id);
        }

        public void Update(T record)
        {
            context.Entry(record).State = EntityState.Modified;
        }

        public void Delete(int id)
        {
            T temp = context.Set<T>().Find(id);
            context.Set<T>().Remove(temp);
        }

        public void Save()
        {
            context.SaveChanges();
        }
    }
}